//
//  PaperMoreView.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/2/28.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperMoreView.h"

@interface PaperMoreView ()

@property (nonatomic, strong) UIView *dropDownView;         // 下啦弹出框

@end

@implementation PaperMoreView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupConfigureView];
    }
    return self;
}

- (void)setupConfigureView {
    self.backgroundColor = [UIColor clearColor];

    UITapGestureRecognizer *singleFingerTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(handleSingleTap:)];
    [self addGestureRecognizer:singleFingerTap];
    
    self.dropDownView.hidden = YES;
    [self loadDropDownSubview];
    [self addSubview:self.dropDownView];
}

- (void)showWithFavorite:(NSUInteger)favorite withAnimation:(BOOL)animation {
    
    UIImageView *imageView = [self.dropDownView viewWithTag:21];
    imageView.image = [UIImage imageNamed:favorite == 0 ? @"PaperMoreImage1" : @"PaperMoreImage3"];
    [self showWithAnimation:animation];
}

- (void)showWithAnimation:(BOOL)animation {
    self.hidden = NO;
    self.dropDownView.hidden = NO;
}

- (void)hiddenWithAnimation:(BOOL)animation {
    self.hidden = YES;
    self.dropDownView.hidden = YES;
}

- (void)loadDropDownSubview {
    CGFloat top = 0;
    NSArray *titles = @[@"纠错", @"收藏", @"分享"];
    for (int i = 0; i < titles.count; i++) {
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, top, 120, 1)];
        lineView.backgroundColor = [UIColor whiteColor];//RGB(22, 170, 170);
        [self.dropDownView addSubview:lineView];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"PaperMoreImage%d", i]]];
        imageView.frame = CGRectMake(16, top + 10, 20, 20);
        imageView.tag = i + 20;
        [self.dropDownView addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(imageView.right + 18, top, 120 - imageView.right - 12, 40)];
        label.text = titles[i];
        label.textColor = [UIColor whiteColor];
        [self.dropDownView addSubview:label];
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, top, 120, 40)];
        btn.tag = i + 10;
        [btn addTarget:self action:@selector(didAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.dropDownView addSubview:btn];
        top = btn.bottom;
    }
}

#pragma mark - btn action

- (void)didAction:(UIButton *)btn {
    if (btn.tag == 10) {
        [self hiddenWithAnimation:YES];
    }
}

#pragma mark - ViewSignalProtocol

- (RACSignal *)getSignalWithDictionary:(NSDictionary *)dict {
    NSString *str = dict[@"action"];
    if (!str)   return [RACSignal empty];
    
    if ([str isEqualToString:@"wrong"]) {               // 纠错
        UIButton *btn = [self.dropDownView viewWithTag:10];
        return [btn rac_signalForControlEvents:UIControlEventTouchUpInside];
    } else if ([str isEqualToString:@"favorite"]) {     // 收藏
        UIButton *btn = [self.dropDownView viewWithTag:11];
        return [btn rac_signalForControlEvents:UIControlEventTouchUpInside];
    } else if ([str isEqualToString:@"share"]) {        // 分享
        UIButton *btn = [self.dropDownView viewWithTag:12];
        return [btn rac_signalForControlEvents:UIControlEventTouchUpInside];
    } else {
        return [RACSignal empty];
    }
}

#pragma makr - gesture

//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
    [self hiddenWithAnimation:YES];
}

#pragma mark - getter

- (UIView *)dropDownView {
    if (_dropDownView == nil) {
        _dropDownView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 120, kNavigationBarHeight, 120, 120)];
        _dropDownView.backgroundColor = MainThemeColor;
    }
    return _dropDownView;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
